const { parkingsModel } = require('./models/parkingsModel');

module.exports.getParkings = async function(query) {
    const { pageSize, currentPage, isChecked, _id, inp, state } = query;
    if (_id) {
        await parkingsModel.updateOne({ _id }, { state: isChecked });
    }
    var data = await parkingsModel.find().populate({
        path: 'lessorsId'
    }).limit(pageSize - '0').skip((currentPage - 1) * pageSize);

    if (inp || state) {
        var data = await parkingsModel.find({ state, number: { $regex: inp, $options: '$i' } }).populate({
            path: 'lessorsId'
        }).limit(pageSize - '0').skip((currentPage - 1) * pageSize);
    }

    const total = data.length;
    return { data, total };
}